#!/bin/bash
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
if [ ! -f "nginx-1.9.9.tar.gz" ]; then
    wget http://nginx.org/download/nginx-1.9.9.tar.gz  
    tar -zxvf  nginx-1.9.9.tar.gz
fi
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx --with-http_gzip_static_module
make
make install

# ============= 修改nginx配置 ==============
HOME="/usr/local/nginx"
cp $HOME/conf/nginx.conf $HOME/conf/nginx.conf.bak
cat >$HOME/conf/nginx.conf << EOF
#user  nginx;

worker_processes  auto;

events {
    worker_connections  65563;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    server_tokens off;
    gzip on;
    gzip_static on;
    gzip_min_length 100k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 4;
    gzip_types text/plain text/css text/javascript application/json application/octet-stream application/javascript application/x-javascript application/xml image/jpeg image/gif image/png model/gltf+json;
    gzip_vary on;

    server {
        listen       80;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';
        if (\$request_method = 'OPTIONS') {
                return 204;
        }
        location / {
            root  $HOME/html/;
            index index.html index.html;
            try_files   / /index.html;
        }
        location /api/ {
            tcp_nodelay     on;
            proxy_set_header Host \$host;
            proxy_set_header X-Real-IP \$remote_addr;
            proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto "https";
            proxy_http_version 1.1;
            proxy_set_header Connection "upgrade";
            proxy_set_header Upgrade \$http_upgrade;
            proxy_pass http://localhost:8089/;
        }
        location /monitor-ws {
            tcp_nodelay     on;
            proxy_set_header Host \$host;
            proxy_set_header X-Real-IP \$remote_addr;
            proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto "https";
            proxy_http_version 1.1;
            proxy_set_header Connection "upgrade";
            proxy_set_header Upgrade \$http_upgrade;
            proxy_pass http://localhost:8888/monitor-ws;
        }
    }
}
EOF
# ============= 设置开机启动一 ==============
# cp nginx /etc/init.d/nginx
# cd /etc/init.d
# chkconfig --add nginx
# chkconfig --level 2345 nginx on

# ============= 设置开机启动二 ==============
# cp nginx /etc/init.d/nginx
# cd /etc/init.d
# chkconfig --add nginx
# chkconfig --level 2345 nginx on
cat >/etc/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=nginx documentation
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=$HOME/sbin/nginx -t -c $HOME/conf/nginx.conf
ExecStart=$HOME/sbin/nginx -c $HOME/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start nginx;
systemctl enable nginx



